<template>
	<view>
		<view class="video-wrap">
			<video v-show="isOpenVideo" id="myVideo" :src="src" @fullscreenchange="fullscreenchange">
				
			</video>
		</view>
	</view>
</template>

<script>
	export default {
		name: "open-media",
		props: {
			src: {
				type: String,
				default () {
					return ''
				}
			},
			is_download: { //1 不可保存  2 可保存
				type: [String, Number],
				default () {
					return 1
				}
			},
		},
		data() {
			return {
				isOpenVideo: false,
				videoContext: null,
				play_duration: 0,
				total_duration: 0,
				touchStartTime: 0, // 记录触摸开始的时间
				isLongPress: false, // 是否长按的标识
			};
		},
		methods: {
			openMedia() {
				//打开视频（全屏播放）
				this.isOpenVideo = true;
				this.videoContext = uni.createVideoContext('myVideo', this);
				this.videoContext.requestFullScreen({
					direction: 0
				});
				this.videoContext.play();
				this.play_duration = 0;
				this.total_duration = 0;
			},
			//退出全屏时停止播放
			fullscreenchange(e) {
				console.log(e);
				if (!e.detail.fullScreen) {
					this.videoContext.pause();
					this.isOpenVideo = false;
					let obj = {
						play_duration: this.play_duration,
						total_duration: this.total_duration
					}
					this.$emit('exitFull', obj);
				}
			},
			pause(e) {

			},
			ended(e) {


			},
			timeupdate(e) {
				if (e.detail.currentTime == 0) return;
				let play_duration = Math.floor(e.detail.currentTime);
				let total_duration = Math.floor(e.detail.duration);
				// console.log(play_duration, total_duration);
				this.play_duration = play_duration;
				this.total_duration = total_duration;
			},
			onTouchStart(event) {
				console.log('onTouchStart:', 1);
				// 记录触摸开始的时间
				this.touchStartTime = event.timeStamp;
			},
			onTouchEnd(event) {
				// 如果触摸结束后未移除监听器，则计算触摸时间
				if (!this.isLongPress) {
					const endTime = event.timeStamp;
					const timeDiff = endTime - this.touchStartTime; // 计算触摸开始到结束的时间差
					// 如果时间差大于500毫秒，则认为是长按事件
					if (timeDiff > 500) {
						this.isLongPress = true;
						// 执行长按后的操作
						console.log('长按事件触发');
						// 处理长按事件
						// ...
					}
				}
			},
			showPops() {
				console.log('is_download:', this.is_download);
				if (this.is_download == 2) {
					let that = this;
					uni.showActionSheet({
						itemList: ['保存视频'],
						success: function(res) {
							console.log('选中了第' + (res.tapIndex + 1) + '个按钮');
							that.downloadVideo();
						},
						fail: function(res) {
							console.log(res.errMsg);
						}
					});
				}
			},
			downloadVideo() {
				uni.showToast({
					title: "下载中",
					icon: "loading"
				})
				uni.downloadFile({
					url: this.src, //文件路径
					success: (res) => {
						const {
							statusCode,
							tempFilePath
						} = res
						if (res.statusCode === 200) {
							uni.saveVideoToPhotosAlbum({
								filePath: tempFilePath,
								success: function(errMsg) {
									uni.showToast({
										mask: true,
										title: '保存成功'
									});
								},
								fail: (errMsg) => {
									uni.showToast({
										mask: true,
										title: '保存失败'
									});
								},
								complete: (errMsg) => {

								}
							});
						}
					},
				})
			},
		}
	}
</script>

<style lang="less">
.video-wrap{
	position: relative;
}
.fixed-box{
	position: fixed;
	bottom: 100px;
	right: 0;
	z-index: 100;
	background: #529eff;
	color: #fff;
	padding: 20upx;
	height: 50px;
}
</style>